﻿using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace ASPControlsExtended.Controls {

    internal class EmbedResource {

        private EmbedResource() {
        }

        public static void EmbedScript(Control control, string scriptResourceName) {
            if (control.Page != null && !(control.Page.ClientScript.IsClientScriptIncludeRegistered("ControlLibraryScript"))) {
                control.Page.ClientScript.RegisterClientScriptInclude("ControlLibraryScript", control.Page.ClientScript.GetWebResourceUrl(control.GetType(), scriptResourceName));
            }
        }

        public static void EmbedCSS(Control control, string cssResourceName) {
            bool styleClassAdded = false;
            foreach (Control cntrl in ((HtmlHead)control.Page.Header).Controls) {
                if (cntrl.ID == "CssClassLiteral") {
                    styleClassAdded = true;
                    break;
                }
            }

            if (styleClassAdded) {
                return;
            }

            string includeTemplate = "<link href='{0}' rel='stylesheet' type='text/css' />";
            string includeLocation = control.Page.ClientScript.GetWebResourceUrl(control.GetType(), cssResourceName);
            using (LiteralControl include = new LiteralControl(String.Format(includeTemplate, includeLocation))) {
                include.ID = "CssClassLiteral";
                ((HtmlHead)control.Page.Header).Controls.Add(include);
            }
        }

    }
}